package com.jysz.certificate.web.filter;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
 * @author kacarich email:javaengine@outlook.com
 *
 */
public class TimeInterceptor extends AbstractInterceptor {

	private static final long serialVersionUID = 1L;
	
	private final Logger LOG = Logger.getLogger(TimeInterceptor.class);

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		
		long startTime = System.currentTimeMillis();
        String result = invocation.invoke();
        long executionTime = System.currentTimeMillis() - startTime;

        StringBuilder message = new StringBuilder(100);
        message.append("Executed action [");
        String namespace = invocation.getProxy().getNamespace();
        if ((namespace != null) && (namespace.trim().length() > 0)) {
            message.append(namespace).append("/");
        }
        message.append(invocation.getProxy().getActionName());
        message.append(".");
        message.append("do");
        message.append("] took ").append(executionTime).append(" ms.");

        LOG.info(message.toString());
        ServletActionContext.getResponse().setDateHeader("Last-Modified", System.currentTimeMillis());
        return result;
	}
}
